package com.yotoodesign.dianba.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
import android.widget.Toast;

import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.Version;

/*
 * Top10歌手服务
 */
public class VersionService extends Pager {
	private static VersionService versionService;
	
	public static VersionService getInstance(){
		if(versionService==null){
			versionService = new VersionService();
		}
		return versionService;
	}
	
	
	public Version prepare(String xmlFile){
		Version version = null;
		File file = new File(xmlFile);
		//如果当前文件不存在，取当前应用内的文件
		boolean exists = file.exists();
		if(!exists){
			Toast.makeText(GlobalApplication.getMainActivity(), "版本失败，请稍候再试！", Toast.LENGTH_SHORT).show();
			return null;
		}
		Log.i(Constant.DIANBA,"file name="+xmlFile);
		VersionService saxVersionService=new VersionService();
		InputStream istream = null;
		if(exists){
			try {
				istream = new FileInputStream(xmlFile);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
		
		
		try {
			version = saxVersionService.getService(istream);
		} catch (Throwable e) {
			Log.i(Constant.DIANBA,">>>>>>>>>>>>>>get "+xmlFile+" error!["+e.getMessage()+"]");
		}
		return version;
	}
	
	
	// 要解析的XML文件以流的方式传进来
	private Version getService(InputStream istream) throws Throwable {
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = saxParserFactory.newSAXParser();
		verHandler verHandler = new verHandler();
		saxParser.parse(istream, verHandler);
		// 当解析过程中遇到符合XML语法规定的语句时就会调用第2个参数所规定的方法
		return verHandler.getVersion();
	}
	
	
	private final class verHandler extends DefaultHandler {
		private Version ver;
		private String TAG;// 存放当前所遍历的元素节点
		
		public Version getVersion(){
			return ver;
		}
		
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			super.characters(ch, start, length);
			if (TAG != null) {
				String data = new String(ch, start, length);
				if (ver != null) {
					if ("ver".equals(TAG)) {
						ver.setVer(data);
					} else if("desc".equals(TAG)) {
						ver.setDesc(data);
					}else if ("update".equals(TAG)) {
						ver.setUpdate(data);
					} else if ("url".equals(TAG)) {
						ver.setUrl(data);
					} 
				}
			}
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			// localName:不含命名空间前缀的标签名
			// qName:含有命名空间前缀的标签名
			// attributes:接收属性值
			if ("version".equals(localName)) {
				ver = new Version();
			}
			
			TAG = localName;
		}


		@Override
		public void endDocument() throws SAXException {
			super.endDocument();
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			super.endElement(uri, localName, qName);
			TAG = null;
		}


		
	}
}
